package com.elecbook.auth.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.elecbook.auth.model.dto.LoginDTO;
import com.elecbook.auth.model.po.User;
import com.elecbook.auth.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;


@Service("auth_password")
public class PasswordAuthService extends AbstractAuthService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    PasswordEncoder passwordEncoder;

    /**
     * description 在书写代码的时候你需要记住，spring-security中默认的DaoProvider是先查询封装权限信息，之后采取校验密码的
     *
     * @author zken
     * CreateDate 2024/11/2 22:58:00
     */
    @Override
    public UserDetails login(LoginDTO loginDTO) {
        String username = loginDTO.getPhone();
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<User>();
        queryWrapper.eq(User::getPhone, username);
        User user = userMapper.selectOne(queryWrapper);
        // 密码模式下，可以继续延续使用原本的provider
        UserDetails userDetails = loadUserByUserPo(user);
        return userDetails;
    }
}
